package com.sucisoft.base.network.callback;

import android.app.Activity;
import android.graphics.Color;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.exception.HttpException;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.sucisoft.base.cache.XHandler;
import com.sucisoft.base.tools.XToast;
import com.sucisoft.base.view.XLoadingDialog;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.List;


/**
 * 2033152950
 * Created by zf on 2017/12/21.
 */

public abstract class DialogGsonCallback<T> extends StringCallback implements ICallback {

    private XLoadingDialog xLoadingDialog;

    private Activity activity;

    private void initDialog(Activity activity) {
        this.activity = activity;
        if (activity != null) {
            xLoadingDialog = XLoadingDialog.with(activity)
                    .setBackgroundColor(Color.parseColor("#FF000000"))
                    .setMessageColor(Color.WHITE)
                    .setCanceled(false);
        }

    }


    public DialogGsonCallback(Activity activity) {
        super();
        initDialog(activity);
    }


    @Override
    public void onStart(Request<String, ? extends Request> request) {
        if (xLoadingDialog != null && !xLoadingDialog.isShowing()) {
            xLoadingDialog.show();
        }
    }

    @Override
    public void onError(Response response) {
        super.onError(response);
        Throwable throwable = response.getException();
        if (throwable != null) throwable.printStackTrace();

        if (throwable instanceof UnknownHostException || throwable instanceof ConnectException) {
            XToast.custom("网络连接失败，请连接网络！", 2000).show();
        } else if (throwable instanceof SocketTimeoutException) {
            XToast.custom("网络请求超时！", 2000).show();
        } else if (throwable instanceof HttpException) {
            XToast.custom("网络无响应！", 2000).show();
        } else if (throwable instanceof IllegalStateException) {
            XToast.custom("非网络请求所造成的错误！", 2000).show();
        }
    }

    @Override
    public void onFinish() {
        if (xLoadingDialog != null && xLoadingDialog.isShowing()) {
            xLoadingDialog.dismiss();
        }
    }

    private Type clazz;

    public void onSuccess(Response response) {
        final String s = response.body().toString();
        if (s.indexOf("html") > 0) {
            Log.i("onSuccess", "error: " + s);
            return;
        }
        Gson gson = new Gson();
        clazz = resolveClass(this);
        if (clazz instanceof List) {
            clazz = new TypeToken<T>() {
            }.getType();
        }
        final T t = gson.fromJson(s, clazz);
        new XHandler().post(new Runnable() {
            @Override
            public void run() {
                Success(t);
            }
        });
    }

    protected abstract void Success(T t);

    /**
     * 获取Resonse的Class
     *
     * @param object this
     * @return (Resonse).Class
     */
    private Type resolveClass(Object object) {
        Type type = object.getClass().getGenericSuperclass();
        Type[] types = ((ParameterizedType) (type)).getActualTypeArguments();
        return types[0];
    }

}
